<?php
Class FT_User {
	private $profile = array();
	private $url = array();
	private $blogs = array();
	private $contacts = array();
	private $messages = array();
	private $extras;
	
	/* CONSTRUCTOR */
	
	public function FT_User($user_id = null, $extras = true) {
		$this->profile['id'] = $user_id;
		$this->extras = $extras;

		$this->FT_Crear_Perfil();
		$this->FT_Crear_URL();

		$this->FT_Crear_Perfil_Extra();
		$this->FT_Crear_Blogs();
		$this->FT_Crear_Contactos();
		//$this->FT_Crear_Mensajes();
	}
	
	/* METODOS PRIVADOS */
	
	/* Crea el perfil del usuario */
	
	private function FT_Crear_Perfil() {
		global $current_user, $bp;
		
		if(is_null($this->profile['id']))
			$this->profile['id'] = $current_user->ID;
		
		if($this->profile['id'] == $current_user->ID) :
			$this->profile['login'] = $current_user->user_login;
			$this->profile['name'] = $bp->loggedin_user->fullname;
			$this->profile['current'] = true;
		else :
			$this->profile['login'] = get_userdata($this->profile['id'])->user_login;
			$this->profile['name'] = bp_core_get_user_displayname($this->profile['id']);
			$this->profile['current'] = false;
		endif;
		
		$this->profile['level'] = get_userdata($this->profile['id'])->user_level;
	}
	
	/* Crea las URL del usuario */
	
	private function FT_Crear_URL() {
		global $bp;
		
		if($this->profile['current'] && $this->extras) :
			$url_user['base'] = $bp->loggedin_user->domain;
			
			foreach($bp->bp_nav as $nav_item) :
				$url_user[$nav_item['css_id']] = $nav_item['link'];
			endforeach;
			
			$url_extra = array(
				'url_'.BP_BLOGS_SLUG => $url_user[BP_BLOGS_SLUG],
				BP_FRIENDS_SLUG => $url_user['base'].BP_FRIENDS_SLUG.'/',
				BP_ACTIVITY_SLUG.'_me' => $url_user[BP_ACTIVITY_SLUG].'just-me/',
				BP_ACTIVITY_SLUG.'_friends' => $url_user[BP_ACTIVITY_SLUG].'my-friends/',
				BP_MESSAGES_SLUG.'_compose' => $url_user[BP_MESSAGES_SLUG].'compose/',
				BP_XPROFILE_SLUG.'_edit' => $url_user[BP_XPROFILE_SLUG].'edit/'
			);
			
			$this->url = array_merge($url_user, $url_extra);
		else :
			$this->url = array(
				'base' => bp_core_get_user_domain($this->profile['id'])
			);
		endif;
	}
	
	/* Crea el perfil extra del usuario */
	
	private function FT_Crear_Perfil_Extra() {
		if($this->extras) :
			$profile = new bp_xprofile_data_template($this->profile['id'], false);
				
			if($profile->has_groups()) :
				while($profile->profile_groups()) :
					$profile->the_profile_group();
					if($profile->has_fields()) :
						while($profile->profile_fields()) :
							$profile->the_profile_field();
							$value = FT_Unserialize($profile->field->data->value);
							if(!empty($value) && !is_null($value)) :
								$field = array(
									'name' => $profile->field->name,
									'value' => $value
								); 
								$profile_extra[sanitize_title($field['name'])] = $field;
							endif;
						endwhile;
					endif;
				endwhile;
				
				$this->profile['extra'] = $profile_extra;
			endif;
		endif;
	}
	
	/* Crea los blogs a los que pertenece el usuario */
	
	private function FT_Crear_Blogs() {
		if($this->profile['current']) :
			$blogs_aux = get_blogs_of_user($this->profile['id']);
			
			if(!empty($blogs_aux)) :
				foreach($blogs_aux as $blog_aux) :
					$blog_aux = (array) $blog_aux;
					if($blog_aux['userblog_id'] != get_site_option('dashboard_blog'))
						$this->blogs[strtolower($blog_aux['blogname'])] = new FT_Blog($blog_aux['userblog_id'], false);
				endforeach;
				
				ksort($this->blogs);
				
				foreach($this->blogs as $blog) :
					$blogs_aux_aux[$blog->FT_Get_Info('id')] = $blog;
				endforeach;
				
				$this->blogs = $blogs_aux_aux;
				
				$this->profile['total_blogs'] = count($this->blogs);
			else :
				$this->profile['total_blogs'] = 0;
				unset($this->blogs);
			endif;
		else :
			unset($this->blogs);
		endif;
	}
	
	/* Crea los contactos del usuario */
	
	private function FT_Crear_Contactos() {
		if($this->profile['current'] && $this->extras) :
			$contacts_id = BP_Friends_Friendship::get_friend_user_ids($this->profile['id']);

			if(!empty($contacts_id)) :
				foreach($contacts_id as $contact_id) :
					$contact_aux = new FT_User($contact_id, false);
					$contacts_aux[strtolower($contact_aux->FT_Get_Info('name'))] = $contact_aux;
				endforeach;
					
				ksort($contacts_aux);
				
				foreach($contacts_aux as $contact_aux) :
					$this->contacts[$contact_aux->FT_Get_Info('id')] = $contact_aux;
				endforeach;
				
				$this->profile['total_contacts'] = count($this->contacts);
			else :
				$this->profile['total_contacts'] = 0;
				unset($this->contacts);
			endif;
		else :
			unset($this->contacts);
		endif;
	}
	
	/* Crea los mensajes del usuario */
	
	private function FT_Crear_Mensajes() {
	}
	
	/* METODOS PUBLICOS */
	
	/* Devuelve la info basica del usuario */
	
	public function FT_Get_Info($info_name = '') {
		if(empty($info_name))
			return $this->profile;
		else
			return $this->profile[sanitize_title($info_name)];
	}
	
	/* Devuelve las URL del usuario */
	
	public function FT_Get_URL($url_name = '') {
		if(empty($url_name))
			return $this->url;
		else
			return $this->url[strtolower($url_name)];
	}
	
	/* Devuelve los blogs (ciudades) al que pertenece el usuario */
	
	public function FT_Get_Blogs($blog_id = null) {
		if(is_null($blog_id))
			return $this->blogs;
		else
			return $this->blogs[$blog_id];
	}
	
	/* Devuelve los contactos del usuario */
	
	public function FT_Get_Contacts($contact_id = null) {
		if(is_null($contact_id))
			return $this->contacts;
		else
			return $this->contacts[$contact_id];
	}
}

/* Imprime o devuelve el avatar del usuario */

function FT_User_Avatar($tamano = null, $echo = true) {
	$user_avatar = get_avatar(FT_Info('user-id', false), $tamano, '', FT_Info('user-name', false));
	
	if($echo) :
		echo $user_avatar;
		return;
	else :
		return $user_avatar;
	endif;
}
?>